use crate::core::base_node::NodeStatus;
use crate::core::action;
use core::base_node::{BaseNode, NodeData, TBaseNode};
use core::behavior_tree::NodeTypeRegister;
use std::collections::HashMap;
use core::tick::Tick;

pub struct Runner {
    base_node: NodeData<BaseNode>
}
impl Runner {
    fn new(config: serde_json::Value)->Result<Runner, String> {
        match BaseNode::new(config) {
            Ok(_n) => {
                Ok(Runner {
                    base_node: NodeData::new(_n)
                })
            }
            Err(_e) => {
                Err(_e)
            }
        }
    }
    pub fn register(container: &mut NodeTypeRegister, name: String) {
        container.register_type(name, Box::new(|config: serde_json::Value, nodes: &mut HashMap<String, Box<TBaseNode>>| {
            match Runner::new(config) {
                Ok(_n) => {
                    nodes.insert(_n.base_node.id.to_string(), Box::new(_n));
                }
                Err(_e) => {
                    println!("~~~~~ {:?}", _e.to_string());
                }
            };
        }));
    }
}
impl TBaseNode for Runner {
    fn get_base_node_ref(&self) -> NodeData<BaseNode> {
        self.base_node.clone()
    }
    fn execute(&self, tick: &mut Tick) -> NodeStatus {
        BaseNode::_execute(self.base_node.clone(), tick, self)
    }
    fn tick(&self, tick: &mut Tick) -> NodeStatus{
        NodeStatus::RUNNING
    }
}